找传奇、传世资源到传世资源站!

C# 模拟登陆百度 例子

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

模拟登陆百度
模拟登陆百度
模拟登陆百度using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Text.RegularExpressions;using System.Web;using Newtonsoft.Json;namespace BaiduLoginDemo{ class Program { private Regex _regex = new Regex(@"\{.*\}", RegexOptions.IgnoreCase); static void Main(string[] args) { new Program().Login("用户名","密码"); } public void Login(string username,string pwd) { var cookieContainer = new CookieContainer(); var token = GetToken(cookieContainer); if (_regex.IsMatch(token)) token = _regex.Match(token).Value; var resultToken = JsonConvert.DeserializeObject<ResultToken>(token); var pubKey = GetPubKey(resultToken.Data.Token, cookieContainer); if (_regex.IsMatch(pubKey)) pubKey = _regex.Match(pubKey).Value; var publicKey = JsonConvert.DeserializeObject<PublicRsaKey>(pubKey); var rsakey = publicKey.Key; var pemToXml = RSAHelper.PemToXml(publicKey.Pubkey); var password = RSAHelper.RSAEncrypt(pemToXml, pwd); var url = "https://passport.baidu.com/v2/api/?login"; var postdata = string.Format("staticpage=http%3A%2F%2Fapp.baidu.com%2Fsfile%2Fv3Jump.html&charset=UTF-8&token={0}&tpl=wise&subpro=&apiver=v3&tt={1}&codestring=&safeflg=0&u=%0D%0A%0D%0Ahttp%3A%2F%2Fapp.baidu.com%2Findex%3Fregdev%3D1&isPhone=false&quick_user=0&logintype=dialogLogin&logLoginType=pc_loginDialog&idc=&loginmerge=true&splogin=newuser&username={2}&password={3}&verifycode=&mem_pass=on&rsakey={4}&crypttype=12&ppui_logintime=426406&callback=parent.bd__pcbs__mwrr8d", resultToken.Data.Token, GetJsTimeSeconds(), HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password), HttpUtility.UrlEncode(rsakey)); PostRequest(url, cookieContainer, postdata); } private string GetToken(CookieContainer cookieContainer) { var url = string.Format(@"https://passport.baidu.com/v2/api/?getapi&tpl=wise&apiver=v3&tt={0}&class=login&logintype=dialogLogin&callback=bd__cbs__v5pvt1", GetJsTimeSeconds()); GetHtml(url, ref cookieContainer, "passport.baidu.com"); return GetHtml(url, ref cookieContainer, "passport.baidu.com"); } private string GetPubKey(string token, CookieContainer cookieContainer) { var url = string.Format("https://passport.baidu.com/v2/getpublickey?token={0}&tpl=wise&apiver=v3&tt={1}&callback=bd__cbs__fwnq4r", token, GetJsTimeSeconds()); var html = GetHtml(url, ref cookieContainer, "passport.baidu.com"); return html; } private static string GetJsTimeSeconds() { return (DateTime.UtcNow - DateTime.Parse("1970-01-01 0:0:0")).TotalMilliseconds.ToString("0"); } private string GetHtml(string url, ref CookieContainer cookie, string host) { HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = null; request.Credentials = CredentialCache.DefaultCredentials; request.Method = "GET"; request.KeepAlive = true; request.Accept = "application/json, text/plain, */*"; request.CookieContainer = cookie; request.AllowAutoRedirect = true; response = (HttpWebResponse)request.GetResponse(); var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string ss = sr.ReadToEnd(); sr.Close(); request.Abort(); response.Close(); return ss; } catch (WebException ex) { return ""; } } private void PostRequest(string url, CookieContainer cookieContainer, string postData) { HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)WebRequest.Create(url);//实例化web访问类 request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"; request.Credentials = CredentialCache.DefaultCredentials; request.Method = "POST"; //数据提交方式为POST request.ContentType = "application/x-www-form-urlencoded"; request.AllowAutoRedirect = false; // 不用需自动跳转 request.Accept = "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.CookieContainer = cookieContainer; request.KeepAlive = true; //提交请求 byte[] postdatabytes = Encoding.UTF8.GetBytes(postData); request.ContentLength = postdatabytes.Length; Stream stream; stream = request.GetRequestStream(); //设置POST 数据 stream.Write(postdatabytes, 0, postdatabytes.Length); stream.Close(); //接收响应 response = (HttpWebResponse)request.GetResponse(); var cookieCollection = response.Cookies;//拿到bduss 说明登录成功 //保存返回cookie //取下一次GET跳转地址 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string content = sr.ReadToEnd(); sr.Close(); } catch (Exception ex) { } finally { if (request != null) request.Abort(); if (response != null) response.Close(); } } } public class PublicRsaKey { [JsonProperty("errno")] public string Errno { get; set; } [JsonProperty("msg")] public string Msg { get; set; } [JsonProperty("pubkey")] public string Pubkey { get; set; } [JsonProperty("key")] public string Key { get; set; } } public class ErrInfo { [JsonProperty("no")] public string No { get; set; } } public class Loginrecord { [JsonProperty("email")] public object[] Email { get; set; } [JsonProperty("phone")] public object[] Phone { get; set; } } public class Data { [JsonProperty("rememberedUserName")] public string RememberedUserName { get; set; } [JsonProperty("codeString")] public string CodeString { get; set; } [JsonProperty("token")] public string Token { get; set; } [JsonProperty("cookie")] public string Cookie { get; set; } [JsonProperty("usernametype")] public string Usernametype { get; set; } [JsonProperty("spLogin")] public string SpLogin { get; set; } [JsonProperty("disable")] public string Disable { get; set; } [JsonProperty("loginrecord")] public Loginrecord Loginrecord { get; set; } } public class ResultToken { [JsonProperty("errInfo")] public ErrInfo ErrInfo { get; set; } [JsonProperty("data")] public Data Data { get; set; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复